package com.wehealth;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.actionbarsherlock.view.*;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.umeng.analytics.MobclickAgent;
import com.wehealth.Home;
import com.wehealth.dm.User;
import com.wehealth.util.Common;
import com.wehealth.util.DataManger;


public class MainActivity extends SlidingFragmentActivity  {

	private boolean misLogin = false;
	private DataManger  mDataManager;
	private User user;
	private Menu_Side menu;
	
	private boolean mLogoAnimalState = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//配置图片加载(单例模式)
		Common.initconfig(this);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
		setBehindContentView(R.layout.menu_side); 		
	    // 设置是否能够使用ActionBar来滑动
        setSlidingActionBarEnabled(true);
        // 初始化滑动视图
        InitSlidingMenu(savedInstanceState);    
        //UMENG统计
		MobclickAgent.setDebugMode(true);
		MobclickAgent.openActivityDurationTrack(true);	
		MobclickAgent.updateOnlineConfig(this);
		
		//这里获取是否登录，通过读取数据库或者其他方式。
		mDataManager = new DataManger(this);
		Log.d("SIMMON","onCreate");
		mLogoAnimalState = true;

	}
	
	public boolean getLogoAnimalState(){
		return mLogoAnimalState;
	}
	
	public void setLogoAnimalState(boolean state){
		Log.d("SIMMON","setLogoAnimalState state="+state);
		mLogoAnimalState = state;
	}
	
	private void updateUserData(){
		user = mDataManager.getUserInfo();
		if(user.getUserId()!= -1){
			misLogin = true;
		}else{
			misLogin = false;
		}
	}
	public boolean getLoginState(){
		updateUserData();
		return misLogin;
	}
	
	public User getUserInfo(){
		updateUserData();
		return user;
	}
	
	public DataManger getDataManger(){
		return mDataManager;
	}
	
	public void updateMenuSide(){
		menu.updateUserState();
	}
	
	
	//初始化侧边栏菜单
	private void InitSlidingMenu(Bundle savedInstanceState) {
        // 如果保存的状态不为空则得到之前保存的Fragment，否则实例化MyFragment
        // 设置主界面视图
        setContentView(R.layout.content_frame);
        
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.content_frame,  new Home())
		.commit();

        
        // 设置滑动菜单的视图
		menu = new Menu_Side();
        setBehindContentView(R.layout.menu_frame);
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.menu_frame, menu)
		.commit();
		
        // 实例话滑动菜单对象
        SlidingMenu sm = getSlidingMenu();
        sm.setMode(SlidingMenu.LEFT);
        // 设置滑动阴影的宽度
        sm.setShadowWidthRes(R.dimen.shadow_width);
        // 设置滑动阴影的图像资源
        sm.setShadowDrawable(R.drawable.shadow);
        // 设置滑动菜单视图的宽度
        sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        // 设置渐入渐出效果的值
        sm.setFadeDegree(0.35f);
        // 设置触摸屏幕的模式
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        
        

      
    }
	
	public void switchContent(Fragment fragment) {
	    if(fragment instanceof Home){
	           getSupportFragmentManager()
	            .beginTransaction()
	            .replace(R.id.content_frame, fragment)
	            .commit();
	    }else{
    		getSupportFragmentManager()
    		.beginTransaction()
    		.replace(R.id.content_frame, fragment).addToBackStack(null)
    		.commit();
	    }

		getSlidingMenu().showContent();
	}
	

	
	public void switchContentHome() {
		getSupportFragmentManager().popBackStack();
	}
	
	
	public void jumpToLayout2()
	{
		setContentView(R.layout.menu_side);
	}
	
	
/*
	//UMENG获取设备ID的方法
	public static String getDeviceInfo(Context context) {
    try{
      org.json.JSONObject json = new org.json.JSONObject();
      android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
          .getSystemService(Context.TELEPHONY_SERVICE);
  
      String device_id = tm.getDeviceId();
      
      android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context.getSystemService(Context.WIFI_SERVICE);
          
      String mac = wifi.getConnectionInfo().getMacAddress();
      json.put("mac", mac);
      
      if( TextUtils.isEmpty(device_id) ){
        device_id = mac;
      }
      
      if( TextUtils.isEmpty(device_id) ){
        device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
      }
      
      json.put("device_id", device_id);
      
      return json.toString();
    }catch(Exception e){
      e.printStackTrace();
    }
  return null;
}
*/
	
    public void openMainActivityDialog(){
         AlertDialog.Builder builder = new AlertDialog.Builder(this); 
         builder.setIcon(null);
         String login_result = getString(R.string.net_connet_fail);
          TextView textview = new TextView(this);
          textview.setText(login_result);
          textview.setTextColor(Color.BLACK);
          textview.setTextSize(18);

          textview.setGravity(Gravity.CENTER);
          builder.setView(textview);
          
          
          builder.setPositiveButton("确定", new AlertDialog.OnClickListener() {
              @Override
              public void onClick(DialogInterface arg0, int arg1) {
                  // TODO Auto-generated method stub
              }
              
          });
          builder.create().show();
          
      }

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mLogoAnimalState = true;
	}


    
}
